package com.wooophone.mpyeah.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.wooophone.mpyeah.biz.BrandBiz;
import com.wooophone.mpyeah.biz.exception.CannotDeleteException;
import com.wooophone.mpyeah.dao.BrandCategoryDao;
import com.wooophone.mpyeah.dao.BrandCommentDao;
import com.wooophone.mpyeah.dao.BrandDao;
import com.wooophone.mpyeah.entity.Brand;
import com.wooophone.mpyeah.entity.BrandCategory;

public class BrandBizImpl implements BrandBiz {

	@Autowired
	private BrandDao brandDao;
	@Autowired
	private BrandCommentDao brandCommentDao;

	public void setBrandDao(BrandDao brandDao) {
		this.brandDao = brandDao;
	}

	@Autowired
	private BrandCategoryDao brandCategoryDao;

	public void setBrandCategoryDao(BrandCategoryDao brandCategoryDao) {
		this.brandCategoryDao = brandCategoryDao;
	}

	@Override
	public Brand save(Brand obj) {
		return brandDao.save(obj);
	}

	@Override
	public int update(Brand obj) {
		return brandDao.update(obj);
	}

	/**删除品牌时先删除其下的评论*/
	@Override
	public int delete(String id) throws CannotDeleteException {
		//brandCommentDao.deleteByBrandId(id);
		return brandDao.delete(id);
	}

	@Override
	public Brand get(String id) {
		return brandDao.get(id);
	}

	@Override
	public List<Brand> list() {
		return brandDao.list();
	}

	@Override
	public List<Brand> list(int offset, int limit) {
		return brandDao.list(offset, limit);
	}

	@Override
	public int count() {
		return brandDao.count();
	}

	@Override
	public List<Brand> find(Map<String, Object> parameter) {
		return brandDao.find(parameter);
	}

	@Override
	public List<Brand> find(Map<String, Object> parameter, int offset, int limit) {
		return brandDao.find(parameter, offset, limit);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return brandDao.count(parameter);
	}

	@Override
	public int count(String keyword) {
		return brandDao.count(keyword);
	}

	@Override
	public List<Brand> search(String keyword, int offset, int limit) {
		return brandDao.search(keyword, offset, limit);
	}

	@Override
	public Map<String, Object> findBrandsAndChildBrandByCid(String cid, int offset,
			int limit) {
		Map<String, Object> map=new HashMap<String, Object>(); 
		BrandCategory brandCategory = brandCategoryDao.get(cid);
		List<String> childredIds = new ArrayList<String>();
		if(brandCategory!=null){
			
		childredIds.add(brandCategory.getId());
		getChildrenIds(brandCategory.getChildren(), childredIds);
		
		int count = brandDao.countByCids(childredIds);
		List<Brand> brands=brandDao.findByCids(childredIds, offset, limit);
		
		map.put(KEY_BRAND_LIST_COUNT, count);
		map.put(KEY_BRAND_LIST_BRANDS, brands);
		}
		return map;
	}

	/**
	 * 递归得到所有子级ID
	 * 
	 * @param children
	 * @param childredIds
	 * @return
	 */
	private List<String> getChildrenIds(List<BrandCategory> children,
			List<String> childredIds) {
		for (BrandCategory brandCategory : children) {
			childredIds.add(brandCategory.getId());
			getChildrenIds(brandCategory.getChildren(), childredIds);
		}
		return childredIds;
	}


	public void setBrandCommentDao(BrandCommentDao brandCommentDao) {
		this.brandCommentDao = brandCommentDao;
	}
	
	

	@Override
	public List<Brand> findRecommendBrand(int offset, int limit) {
		
		return brandDao.findRecommendBrand(offset,limit);
		
	}

	@Override
	public Brand findByName(String name) {
		return brandDao.findByName(name);
	}

	@Override
	public List<BrandCategory> findCategoryList(String id) {
		return brandDao.findCategoryList(id);
	}


}
